/**
 * This file is part of AppStoreBot.
 *
 *  AppStoreBot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AppStoreBot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AppStoreBot.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package lu.pragmaconsult.appstorebot.itunes;

import static lu.pragmaconsult.appstorebot.itunes.Constants.AJAX_NAME;
import static lu.pragmaconsult.appstorebot.itunes.Constants.DEFAULT_VENDOR_PAGE;
import static lu.pragmaconsult.appstorebot.itunes.Constants.PORT;
import static lu.pragmaconsult.appstorebot.itunes.Constants.PROTOCOL;
import static lu.pragmaconsult.appstorebot.itunes.Constants.REPORTING_HOST;
import static lu.pragmaconsult.appstorebot.itunes.Constants.VENDOR_DEFAULT_URL;
import static lu.pragmaconsult.appstorebot.itunes.Constants.VIEW_STATE;
import lu.pragmaconsult.appstorebot.BotContext;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO: Type comment
 * @author Wilfried Vandenberghe (wilfried.vandenberghe at pragmaconsult.lu)
 */
class SalesAndTrendPageBotState extends AbstractBotState {
	
	private static final Logger logger = LoggerFactory.getLogger(SalesAndTrendPageBotState.class);
	
	@Override
	protected HttpMethod createHttpMethod(BotContext context, HttpClient httpClient) {
		if(context.isVerbose()) {
			logger.info("Accessing sales report web page");
		}
		
		if(LoggerUtils.isDebugEnabled()) {
			logger.info("viewState        : " + context.getContextAttribute(VIEW_STATE));
			logger.info("defaultVandorPage: " + context.getContextAttribute(DEFAULT_VENDOR_PAGE));
			logger.info("ajaxName         : " + context.getContextAttribute(AJAX_NAME));
		}
		
		httpClient.getHostConfiguration().setHost(REPORTING_HOST, PORT, PROTOCOL);
		
		PostMethod post = new PostMethod(VENDOR_DEFAULT_URL);
		
		NameValuePair ajax = new NameValuePair("AJAXREQUEST", context.getContextAttribute(AJAX_NAME));
		NameValuePair viewState = new NameValuePair("javax.faces.ViewState", context.getContextAttribute(VIEW_STATE));
		NameValuePair defaultVendorPage = new NameValuePair(DEFAULT_VENDOR_PAGE, context.getContextAttribute(DEFAULT_VENDOR_PAGE));
		NameValuePair defaultVendorPageId = new NameValuePair("defaultVendorPage:" + context.getContextAttribute(DEFAULT_VENDOR_PAGE),
				"defaultVendorPage:" + context.getContextAttribute(DEFAULT_VENDOR_PAGE));
		post.setRequestBody(new NameValuePair[] { ajax, viewState, defaultVendorPage, defaultVendorPageId });
		
		return post;
	}
	
	@Override
	protected void processResponseBody(BotContext context, HttpMethod method) {
		// nothing to do
	}
	
	@Override
	protected BotState getNextState() {
		return new DashboardBotState();
	}

}
